//
//  ZWStatusPictureView.swift
//  ZWWeibo
//
//  Created by flyiron on 16/4/22.
//  Copyright © 2016年 flyiron. All rights reserved.
//

import UIKit
// 每一个条目之间的间距
let ZWStatusPictureItemMargin: CGFloat = 5
// 每一个条目的宽高 (collectionView item 的宽高，尽量设置成整数)
let ZWStatusPictureItemWH: CGFloat = {
    let i = Int((SCREENW - 2 * ZWStatusCellMargin - 2 * ZWStatusPictureItemMargin) / 3)
    return CGFloat(i)
}()

class ZWStatusPictureView: UICollectionView {
    var pic_urls:[ZWStatusPhotoInfo]?{
        didSet{
            reloadData()

//            reloadSections(NSIndexSet(indexesInRange: NSMakeRange(0,1)))
        }
    }
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: layout)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        backgroundColor = UIColor(white: 245/255, alpha: 1)
        registerClass(ZWStatusPictureViewCell.self, forCellWithReuseIdentifier: "pictureCell")
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSize(width: ZWStatusPictureItemWH, height: ZWStatusPictureItemWH)
        layout.minimumInteritemSpacing = ZWStatusPictureItemMargin
        layout.minimumLineSpacing = ZWStatusPictureItemMargin
        self.dataSource = self
        
    }

}
extension ZWStatusPictureView:UICollectionViewDataSource {
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pic_urls?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("pictureCell", forIndexPath: indexPath) as! ZWStatusPictureViewCell
        cell.picurl = pic_urls![indexPath.row]
        return cell
    }
}
class ZWStatusPictureViewCell: UICollectionViewCell {
    var picurl:ZWStatusPhotoInfo?{
        didSet{
            cellImageView.sd_setImageWithURL(NSURL(string: picurl!.thumbnail_pic ?? ""), placeholderImage: UIImage(named: "timeline_image_placeholder"))
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
//        backgroundColor = RandomColor
        contentView.addSubview(cellImageView)
        
        cellImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView)
        }
        
    }
    
    private lazy var cellImageView:UIImageView = {
        let view = UIImageView(image: UIImage(named: "timeline_image_placeholder"))
        view.contentMode = .ScaleAspectFill
        view.clipsToBounds = true
        return view
    }()
    
}